package com.huifaapp.mall.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.huifaapp.mall.R;
import com.huifaapp.mall.bean.AlreadyPayBean;

import java.util.List;

/**
 * Created by mac on 2017/12/11.
 */

public class AlreadyPayAdapter extends RecyclerView.Adapter{


    private Context context;
    private MyViewHolder viewHolder;
    private AlreadyPayBean.DataBean goods;
    private List<AlreadyPayBean.DataBean.OrderItemBean> orderdata;

    public AlreadyPayAdapter(Context context, AlreadyPayBean.DataBean goods, List<AlreadyPayBean.DataBean.OrderItemBean> orderdata) {
        this.context = context;
        this.goods = goods;
        this.orderdata = orderdata;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view =  LayoutInflater.from(context).inflate(R.layout.alreadypay_item,null);
        viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        /*自定义item的点击事件不为null，设置监听事件*/
        if (itemClickListener != null) {
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClickListener.OnItemClickListener(viewHolder.itemView, viewHolder.getLayoutPosition());
                }
            });
        }
//        Glide.with(context).load(goods.get(position).getGoodsImg()).into(viewHolder.im_shop);
//        viewHolder.shop_name.setText(goods.get(position).getGoodsName());
//        viewHolder.shop_color.setText(goods.get(position).getGoodsColor());
//        viewHolder.shop_size.setText(goods.get(position).getGoodsSize());
        AlreadyPayBean.DataBean.OrderItemBean orderItemBean = orderdata.get(position);
        Glide.with(context).load(orderItemBean.getGoodsImg()).into(viewHolder.im_shop);
        viewHolder.shop_name.setText(orderItemBean.getGoodsName());
        AlreadyPayBean.DataBean.OrderItemBean.ParamBean param = orderItemBean.getParam();
        String color = param.get颜色() == null ? "无" : param.get颜色();   //三元运算符   判断param.getColor()是否为null  如果是null则取 : 前面的值，否则后面的值
        String brand = param.get尺寸() == null ? "**" : param.get尺寸();   //同上
        viewHolder.shop_color.setText(color + "；");   //商品的颜色
        viewHolder.shop_size.setText(brand);
    }

    @Override
    public int getItemCount() {
        return orderdata.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView im_shop;
        TextView shop_name;
        TextView shop_color;
        TextView shop_size;
        TextView shop_xj;
        TextView shop_yj;
        TextView shop_num;
        public  Button bt_money;
        public Button bt_txfh;

        public  MyViewHolder(View itemView) {
            super(itemView);
            im_shop = (ImageView) itemView.findViewById(R.id.already_shopimg);
            shop_name = (TextView) itemView.findViewById(R.id.alredy_shopname);
            shop_color = (TextView) itemView.findViewById(R.id.already_shopcolor);
            shop_size = (TextView) itemView.findViewById(R.id.already_shopsize);
            bt_money = (Button) itemView.findViewById(R.id.already_btmoney);
            shop_xj = (TextView) itemView.findViewById(R.id.already_xj);
            shop_yj = (TextView) itemView.findViewById(R.id.already_yj);
            shop_num = (TextView) itemView.findViewById(R.id.already_num);
            bt_txfh = (Button) itemView.findViewById(R.id.tixingfahuo);
        }

        public void setData(Context context, AlreadyPayBean.DataBean.OrderItemBean orderItemBean) {
            Glide.with(context).load(orderItemBean.getGoodsImg()).into(im_shop);
            shop_name.setText(orderItemBean.getGoodsName());
            AlreadyPayBean.DataBean.OrderItemBean.ParamBean param = orderItemBean.getParam();
            String color = param.get颜色() == null ? "无" : param.get颜色();   //三元运算符   判断param.getColor()是否为null  如果是null则取 : 前面的值，否则后面的值
             String brand = param.get尺寸() == null ? "**" : param.get尺寸();   //同上
            shop_color.setText(color + ";");   //商品的颜色
            shop_size.setText(brand);
            shop_num.setText("x"+orderItemBean.getCount()+"");
            shop_xj.setText("¥"+orderItemBean.getPrice()+"");
            shop_yj.setText("¥"+orderItemBean.getOrginPrice()+"");
            bt_txfh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ToastUtils.showShort("提醒发货成功");
                }
            });
        }
    }



    public interface MyOnItemClickListener{
        void OnItemClickListener(View view, int position);
    }
    private MyOnItemClickListener itemClickListener;
    public void setOnItemClickListener(MyOnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

}
